﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Microsoft.Practices.Unity;
using Entities;
using Repositories;
using ServiceContracts;

namespace Services
{
    public partial class NorthwindService : INorthwindService
    {	
    	private Func<INorthwindRepository> _northwindRepositoryFactory;
            
    	public NorthwindService(IUnityContainer unityContainer)
    	{
    		_northwindRepositoryFactory = () => unityContainer.Resolve<INorthwindRepository>();
    	}
    
    	public List<Category> GetCategories()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.Categories.ToList();
    		}
    	}
    
    	public List<Category> GetCategories(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCategories(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public Category GetCategory(System.Int32 Id)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCategory(Id);
    		}
    	}	 
    
    	public List<Product> GetProductsFromCategory(Category entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetProductsFromCategory(entity);
    		}
    	}
    	
    
    
    	public List<Customer> GetCustomers()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.Customers.ToList();
    		}
    	}
    
    	public List<Customer> GetCustomers(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomers(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public Customer GetCustomer(System.String CustomerID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomer(CustomerID);
    		}
    	}	 
    
    	public List<Order> GetOrdersFromCustomer(Customer entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrdersFromCustomer(entity);
    		}
    	}
    	
    	public List<CustomerDemographic> GetCustomerDemographicsFromCustomer(Customer entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomerDemographicsFromCustomer(entity);
    		}
    	}
    	
    	public Member GetMemberFromCustomer(Customer entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetMemberFromCustomer(entity);
    		}
    	}
    	
    
    
    	public List<CustomerDemographic> GetCustomerDemographics()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.CustomerDemographics.ToList();
    		}
    	}
    
    	public List<CustomerDemographic> GetCustomerDemographics(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomerDemographics(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public CustomerDemographic GetCustomerDemographic(System.String CustomerTypeID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomerDemographic(CustomerTypeID);
    		}
    	}	 
    
    	public List<Customer> GetCustomersFromCustomerDemographic(CustomerDemographic entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomersFromCustomerDemographic(entity);
    		}
    	}
    	
    
    
    	public List<Employee> GetEmployees()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.Employees.ToList();
    		}
    	}
    
    	public List<Employee> GetEmployees(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployees(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public Employee GetEmployee(System.Int32 EmployeeID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployee(EmployeeID);
    		}
    	}	 
    
    	public List<Employee> GetEmployeesFromEmployee(Employee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployeesFromEmployee(entity);
    		}
    	}
    	
    	public Employee GetManagerFromEmployee(Employee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetManagerFromEmployee(entity);
    		}
    	}
    	
    	public List<Order> GetOrdersFromEmployee(Employee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrdersFromEmployee(entity);
    		}
    	}
    	
    
    
    	public List<EmployeeInActivity> GetEmployeeInActivities()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.EmployeeInActivities.ToList();
    		}
    	}
    
    	public List<EmployeeInActivity> GetEmployeeInActivities(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployeeInActivities(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public EmployeeInActivity GetEmployeeInActivity(System.Int32 EmployeeID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployeeInActivity(EmployeeID);
    		}
    	}	 
    
    	public List<Employee> GetEmployeesFromEmployeeInActivity(EmployeeInActivity entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployeesFromEmployeeInActivity(entity);
    		}
    	}
    	
    	public Employee GetManagerFromEmployeeInActivity(EmployeeInActivity entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetManagerFromEmployeeInActivity(entity);
    		}
    	}
    	
    	public List<Order> GetOrdersFromEmployeeInActivity(EmployeeInActivity entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrdersFromEmployeeInActivity(entity);
    		}
    	}
    	
    
    
    	public List<Member> GetMembers()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.Members.ToList();
    		}
    	}
    
    	public List<Member> GetMembers(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetMembers(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public Member GetMember(System.String CustomerID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetMember(CustomerID);
    		}
    	}	 
    
    	public Customer GetCustomerFromMember(Member entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomerFromMember(entity);
    		}
    	}
    	
    
    
    	public List<Order> GetOrders()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.Orders.ToList();
    		}
    	}
    
    	public List<Order> GetOrders(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrders(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public Order GetOrder(System.Int32 OrderID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrder(OrderID);
    		}
    	}	 
    
    	public Customer GetCustomerFromOrder(Order entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCustomerFromOrder(entity);
    		}
    	}
    	
    	public List<OrderDetail> GetOrderDetailsFromOrder(Order entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetailsFromOrder(entity);
    		}
    	}
    	
    	public Employee GetEmployeeFromOrder(Order entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployeeFromOrder(entity);
    		}
    	}
    	
    
    
    	public List<OrderDetail> GetOrderDetailSet()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.OrderDetailSet.ToList();
    		}
    	}
    
    	public List<OrderDetail> GetOrderDetailSet(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetailSet(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public OrderDetail GetOrderDetail(System.Int32 OrderID, System.Int32 ProductID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetail(OrderID, ProductID);
    		}
    	}	 
    
    	public Order GetOrderFromOrderDetail(OrderDetail entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderFromOrderDetail(entity);
    		}
    	}
    	
    	public Product GetProductFromOrderDetail(OrderDetail entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetProductFromOrderDetail(entity);
    		}
    	}
    	
    	public List<OrderDetailComment> GetOrderDetailCommentsFromOrderDetail(OrderDetail entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetailCommentsFromOrderDetail(entity);
    		}
    	}
    	
    
    
    	public List<OrderDetailComment> GetOrderDetailComments()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.OrderDetailComments.ToList();
    		}
    	}
    
    	public List<OrderDetailComment> GetOrderDetailComments(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetailComments(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public OrderDetailComment GetOrderDetailComment(System.Int32 Id)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetailComment(Id);
    		}
    	}	 
    
    	public OrderDetail GetOrderDetailsFromOrderDetailComment(OrderDetailComment entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetailsFromOrderDetailComment(entity);
    		}
    	}
    	
    
    
    	public List<OutEmployee> GetOutEmployees()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.OutEmployees.ToList();
    		}
    	}
    
    	public List<OutEmployee> GetOutEmployees(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOutEmployees(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public OutEmployee GetOutEmployee(System.Int32 EmployeeID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOutEmployee(EmployeeID);
    		}
    	}	 
    
    	public List<Employee> GetEmployeesFromOutEmployee(OutEmployee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployeesFromOutEmployee(entity);
    		}
    	}
    	
    	public Employee GetManagerFromOutEmployee(OutEmployee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetManagerFromOutEmployee(entity);
    		}
    	}
    	
    	public List<Order> GetOrdersFromOutEmployee(OutEmployee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrdersFromOutEmployee(entity);
    		}
    	}
    	
    
    
    	public List<Product> GetProducts()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.Products.ToList();
    		}
    	}
    
    	public List<Product> GetProducts(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetProducts(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public Product GetProduct(System.Int32 ProductID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetProduct(ProductID);
    		}
    	}	 
    
    	public List<OrderDetail> GetOrderDetailsFromProduct(Product entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrderDetailsFromProduct(entity);
    		}
    	}
    	
    	public Category GetCategoryFromProduct(Product entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetCategoryFromProduct(entity);
    		}
    	}
    	
    
    
    	public List<RetiredEmployee> GetRetiredEmployees()
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.RetiredEmployees.ToList();
    		}
    	}
    
    	public List<RetiredEmployee> GetRetiredEmployees(List<string> include, string filter, string orderBy, int? skip, int? take)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetRetiredEmployees(include, filter, orderBy, skip, take).ToList();
    		}
    	}
    	 
    	public RetiredEmployee GetRetiredEmployee(System.Int32 EmployeeID)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetRetiredEmployee(EmployeeID);
    		}
    	}	 
    
    	public List<Employee> GetEmployeesFromRetiredEmployee(RetiredEmployee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetEmployeesFromRetiredEmployee(entity);
    		}
    	}
    	
    	public Employee GetManagerFromRetiredEmployee(RetiredEmployee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetManagerFromRetiredEmployee(entity);
    		}
    	}
    	
    	public List<Order> GetOrdersFromRetiredEmployee(RetiredEmployee entity)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			return context.GetOrdersFromRetiredEmployee(entity);
    		}
    	}
    	
    
    
    	public List<string> GenerateErrors(ClientContext clientContext)
    	{
    		List<string> value = new List<string>();
    		foreach (var entity in clientContext.Customers)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.OrderDetailSet)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.Orders)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.Products)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.CustomerDemographics)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.Categories)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.Employees)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.Members)
    			value.AddRange(entity.Errors);
    		foreach (var entity in clientContext.OrderDetailComments)
    			value.AddRange(entity.Errors);
    		return value;
    	}
    
    	public ClientContext SaveChanges(ClientContext clientContext, bool validateBeforeSaving)
    	{
    		using (var context = _northwindRepositoryFactory())
    		{
    			List<SaveError> errors = new List<SaveError>();
    			foreach (var entity in clientContext.Customers)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.OrderDetailSet)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.Orders)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.Products)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.CustomerDemographics)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.Categories)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.Employees)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.Members)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			foreach (var entity in clientContext.OrderDetailComments)
    			{
          			List<string> entityErrors;
    				if (validateBeforeSaving && (entityErrors = entity.Errors).Any())
    					errors.Add(new SaveError { Entity = entity, Errors = entityErrors });
    				else
    					context.ApplyChanges(entity);
    			}
    			if (errors.Any())
    				throw new FaultException<List<SaveError>>(errors);
    			context.SaveChanges();
    			return clientContext;
    		}
    	}
    }
}
